Add a POST endpoint called handle_details_post that Agents' details views can use to send data to the Agent from the UI. Add a new ManualEventAgent that uses the handle_details_post functionality to create simple events on demand for testing / debugging.

Andrew Cantino 11 years ago
parent
commit
8d7dde6fb5

+ 7 - 6
app/assets/javascripts/application.js.coffee.erb

@@ -8,13 +8,14 @@
8 8
 #= require ./worker-checker
9 9
 #= require_self
10 10
 
11
-setupJsonEditor = ->
11
+window.setupJsonEditor = ($editor = $(".live-json-editor")) ->
12 12
   JSONEditor.prototype.ADD_IMG = '<%= image_path 'json-editor/add.png' %>'
13 13
   JSONEditor.prototype.DELETE_IMG = '<%= image_path 'json-editor/delete.png' %>'
14
-  if $(".live-json-editor").length
15
-    window.jsonEditor = new JSONEditor($(".live-json-editor"), 400, 500)
16
-    window.jsonEditor.doTruncation true
17
-    window.jsonEditor.showFunctionButtons()
14
+  if $editor.length
15
+    jsonEditor = new JSONEditor($editor, $editor.data('width') || 400, $editor.data('height') || 500)
16
+    jsonEditor.doTruncation true
17
+    jsonEditor.showFunctionButtons()
18
+    return jsonEditor
18 19
 
19 20
 hideSchedule = ->
20 21
   $(".schedule-region select").hide()
@@ -45,7 +46,7 @@ showEventDescriptions = ->
45 46
 
46 47
 $(document).ready ->
47 48
   # JSON Editor
48
-  setupJsonEditor()
49
+  window.jsonEditor = setupJsonEditor()
49 50
 
50 51
   # Select2 Selects
51 52
   $(".select2").select2(width: 'resolve')

+ 10 - 0
app/controllers/agents_controller.rb

@@ -8,6 +8,16 @@ class AgentsController < ApplicationController
8 8
     end
9 9
   end
10 10
 
11
+  def handle_details_post
12
+    @agent = current_user.agents.find(params[:id])
13
+    if @agent.respond_to?(:handle_details_post)
14
+      render :json => @agent.handle_details_post(params) || {}
15
+    else
16
+      @agent.error "#handle_details_post called on an instance of #{@agent.class} that does not define it."
17
+      head 500
18
+    end
19
+  end
20
+
11 21
   def run
12 22
     agent = current_user.agents.find(params[:id])
13 23
     Agent.async_check(agent.id)

+ 32 - 0
app/models/agents/manual_event_agent.rb

@@ -0,0 +1,32 @@
1
+module Agents
2
+  class ManualEventAgent < Agent
3
+    cannot_be_scheduled!
4
+    cannot_receive_events!
5
+
6
+    description <<-MD
7
+      Use this Agent to manually create Events for testing or other purposes.
8
+    MD
9
+
10
+    event_description "User determined"
11
+
12
+    def default_options
13
+      { "no options" => "are needed" }
14
+    end
15
+
16
+    def handle_details_post(params)
17
+      if params[:payload]
18
+        create_event(:payload => params[:payload])
19
+        { :success => true }
20
+      else
21
+        { :success => false, :error => "You must provide a JSON payload" }
22
+      end
23
+    end
24
+
25
+    def working?
26
+      true
27
+    end
28
+
29
+    def validate_options
30
+    end
31
+  end
32
+end

+ 42 - 0
app/views/agents/agent_views/manual_event_agent/_show.html.erb

@@ -0,0 +1,42 @@
1
+<h3>Manually Create Events</h3>
2
+
3
+<h4 id='event-creation-status'></h4>
4
+
5
+<%= form_tag handle_details_post_agent_path(@agent), :id => "create-event-form" do %>
6
+  <div class="control-group">
7
+    <textarea rows="10" id="payload" name="payload" class="span8 payload-editor" data-height="200">
8
+      {}
9
+    </textarea>
10
+  </div>
11
+
12
+  <div class='form-actions' style='clear: both'>
13
+    <%= submit_tag "Submit", :class => "btn btn-primary" %>
14
+  </div>
15
+<% end %>
16
+
17
+<script>
18
+  $(function () {
19
+    var payloadJsonEditor = window.setupJsonEditor($(".payload-editor"));
20
+    $("#create-event-form").submit(function (e) {
21
+      e.preventDefault();
22
+      var $form = $("#create-event-form");
23
+      var $status = $("#event-creation-status");
24
+      $.ajax({
25
+        url: $form.attr('action'),
26
+        method: "post",
27
+        data: { payload: JSON.parse($form.find("textarea").val()) },
28
+        dataType: "JSON",
29
+        success: function(json) {
30
+          if (json.success) {
31
+            $status.text("Success!");
32
+          } else {
33
+            $status.text("An error occurred: " + json.error);
34
+          }
35
+        },
36
+        error: function(response) {
37
+          $status.text("An error occurred: " + response.responseText)
38
+        }
39
+      });
40
+    });
41
+  });
42
+</script>

+ 2 - 2
app/views/agents/show.html.erb

@@ -132,12 +132,12 @@
132 132
 
133 133
             <p>
134 134
               <b>Options:</b>
135
-              <pre><%= JSON.pretty_generate @agent.options %></pre>
135
+              <pre><%= JSON.pretty_generate @agent.options || {} %></pre>
136 136
             </p>
137 137
 
138 138
             <p>
139 139
               <b>Memory:</b>
140
-              <pre><%= JSON.pretty_generate @agent.memory %></pre>
140
+              <pre><%= JSON.pretty_generate @agent.memory || {} %></pre>
141 141
             </p>
142 142
           </div>
143 143
         </div>

+ 1 - 0
app/views/events/index.html.erb

@@ -16,6 +16,7 @@
16 16
         </tr>
17 17
 
18 18
       <% @events.each do |event| %>
19
+        <% next unless event.agent %>
19 20
         <tr>
20 21
           <td><%= link_to event.agent.name, agent_path(event.agent) %></td>
21 22
           <td><%= time_ago_in_words event.created_at %> ago</td>

+ 1 - 1
app/views/events/show.html.erb

@@ -7,7 +7,7 @@
7 7
 
8 8
       <p>
9 9
         <b>Payload:</b>
10
-        <pre><%= JSON.pretty_generate @event.payload %></pre>
10
+        <pre><%= JSON.pretty_generate @event.payload || {} %></pre>
11 11
       </p>
12 12
 
13 13
       <% if @event.lat && @event.lng %>

+ 1 - 0
config/routes.rb

@@ -2,6 +2,7 @@ Huginn::Application.routes.draw do
2 2
   resources :agents do
3 3
     member do
4 4
       post :run
5
+      post :handle_details_post
5 6
       delete :remove_events
6 7
     end
7 8
 

+ 16 - 0
spec/controllers/agents_controller_spec.rb

@@ -18,6 +18,22 @@ describe AgentsController do
18 18
     end
19 19
   end
20 20
 
21
+  describe "POST handle_details_post" do
22
+    it "passes control to handle_details_post on the agent" do
23
+      sign_in users(:bob)
24
+      post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => "bar" }
25
+      JSON.parse(response.body).should == { "success" => true }
26
+      agents(:bob_manual_event_agent).events.last.payload.should == { :foo => "bar" }
27
+    end
28
+
29
+    it "can only be accessed by the Agent's owner" do
30
+      sign_in users(:jane)
31
+      lambda {
32
+        post :handle_details_post, :id => agents(:bob_manual_event_agent).to_param, :payload => { :foo => :bar }
33
+      }.should raise_error(ActiveRecord::RecordNotFound)
34
+    end
35
+  end
36
+
21 37
   describe "GET show" do
22 38
     it "only shows Agents for the current user" do
23 39
       sign_in users(:bob)

+ 5 - 0
spec/fixtures/agents.yml

@@ -92,3 +92,8 @@ bob_twitter_user_agent:
92 92
       :oauth_token => "---",
93 93
       :oauth_token_secret => "---"
94 94
     }.to_yaml.inspect %>
95
+
96
+bob_manual_event_agent:
97
+  type: Agents::ManualEventAgent
98
+  user: bob
99
+  name: "Bob's event testing agent"

+ 1 - 5
spec/models/agents/peak_detector_agent_spec.rb

@@ -69,10 +69,6 @@ describe Agents::PeakDetectorAgent do
69 69
                                   :pattern => { :filter => "something" })
70 70
       @agent.memory[:peaks][:something].length.should == 2
71 71
     end
72
-
73
-    it "works on real world data" do
74
-      pending "need examples"
75
-    end
76 72
   end
77 73
 
78 74
   describe "validation" do
@@ -95,4 +91,4 @@ describe Agents::PeakDetectorAgent do
95 91
       @agent.should_not be_valid
96 92
     end
97 93
   end
98
-end
94
+end